//
//  LoginViewController.swift
//  CustomSwiftProject
//
//  Created by 岚海网络 on 2019/4/8.
//  Copyright © 2019 ljm. All rights reserved.
//

import UIKit
import SnapKit

class LoginViewController: UIViewController, UITextFieldDelegate {
    
    var logoV : UIImageView!
    var loginBtn : UIButton!
    var registerBtn : UIButton!
    var accountTF : UITextField!
    var passwordTF : UITextField!
    var enterBtn : UIButton!
    var accountView : UIView!
    var passwordView : UIView!
    var verificationCodeTF : UITextField!
    var verificationCodeView : UIView!
    var verificationCodeGetBtn : UIButton!
    
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        createWithUI()
    }
    
    
    func createWithUI() {
        view.backgroundColor = .white
        
        logoV = UIImageView.init()
        logoV.image = UIImage.init(named: "logo")
        view.addSubview(logoV)
        logoV.snp.makeConstraints{
            $0.centerX.equalTo(view.snp_centerX)
            $0.centerY.equalTo(view.snp_centerY).offset(-220)
            $0.width.height.equalTo(100)
        }
        
        
        loginBtn = UIButton.init()
        loginBtn.setTitle("登录", for: .normal)
        loginBtn.setTitleColor(.orange, for: .normal)
        loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        loginBtn.tag = 0
        loginBtn.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
        view.addSubview(loginBtn)
        loginBtn.snp.makeConstraints{
            $0.left.equalTo(logoV.snp_left).offset(-20)
            $0.top.equalTo(logoV.snp_bottom).offset(20)
        }
        
        registerBtn = UIButton.init()
        registerBtn.setTitle("注册", for: .normal)
        registerBtn.setTitleColor(.lightGray, for: .normal)
        registerBtn.titleLabel?.font = loginBtn.titleLabel?.font
        registerBtn.tag = 1
        registerBtn.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
        view.addSubview(registerBtn)
        registerBtn.snp.makeConstraints{
            $0.right.equalTo(logoV.snp_right).offset(20)
            $0.top.equalTo(loginBtn.snp_top)
        }
        
        
        accountTF = UITextField.init()
        accountTF.delegate = self
        accountTF.backgroundColor = .white
        accountTF.placeholder = "手机号"
        accountTF.keyboardType = .numberPad
        accountTF.textAlignment = .left
        accountTF.textColor = .black
        accountTF.font = .systemFont(ofSize: 16)
        view.addSubview(accountTF)
        accountTF.snp.makeConstraints{
            $0.top.equalTo(loginBtn.snp_bottom).offset(20)
            $0.left.equalTo(view.snp_left).offset(50)
            $0.right.equalTo(view.snp_right).offset(-50)
            $0.height.equalTo(30)
        }
        
        
        accountView = UIView.init()
        accountView.backgroundColor = .lightGray
        view.addSubview(accountView)
        accountView.snp.makeConstraints{
            $0.left.bottom.right.equalTo(accountTF)
            $0.height.equalTo(1)
        }
        
        
        
        passwordTF = UITextField.init()
        passwordTF.delegate = self
        passwordTF.backgroundColor = .white
        passwordTF.placeholder = "密码"
        passwordTF.keyboardType = .default
        passwordTF.textAlignment = .left
        passwordTF.textColor = .black
        passwordTF.font = accountTF.font
        view.addSubview(passwordTF)
        passwordTF.snp.makeConstraints{
            $0.top.equalTo(accountTF.snp_bottom).offset(30)
            $0.left.right.height.equalTo(accountTF)
        }
        
        
        passwordView = UIView.init()
        passwordView.backgroundColor = .lightGray
        view.addSubview(passwordView)
        passwordView.snp.makeConstraints{
            $0.left.bottom.right.equalTo(passwordTF)
            $0.height.equalTo(1)
        }
        
        
        
        enterBtn = UIButton.init()
        enterBtn.backgroundColor = .orange
        enterBtn.setTitle("登录", for: .normal)
        enterBtn.setTitleColor(.white, for: .normal)
        enterBtn.titleLabel?.font = .systemFont(ofSize: 16)
        enterBtn.layer.cornerRadius = 20
        enterBtn.layer.masksToBounds = true
        enterBtn.tag = 3
        enterBtn.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
        view.addSubview(enterBtn)
        enterBtn.snp.makeConstraints{
            $0.top.equalTo(passwordView.snp_bottom).offset(90)
            $0.centerX.equalTo(view.snp_centerX)
            $0.width.equalTo(120)
            $0.height.equalTo(40)
        }
        
        
        verificationCodeGetBtn = UIButton.init()
        verificationCodeGetBtn.isHidden = true
        verificationCodeGetBtn.backgroundColor = .orange
        verificationCodeGetBtn.setTitle("验证码", for: .normal)
        verificationCodeGetBtn.setTitleColor(.white, for: .normal)
        verificationCodeGetBtn.titleLabel?.font = .systemFont(ofSize: 14)
        verificationCodeGetBtn.layer.cornerRadius = 15
        verificationCodeGetBtn.layer.masksToBounds = true
        verificationCodeGetBtn.tag = 4
        verificationCodeGetBtn.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
        view.addSubview(verificationCodeGetBtn)
        verificationCodeGetBtn.snp.makeConstraints{
            $0.top.equalTo(passwordView.snp_bottom).offset(30)
            $0.right.equalTo(passwordView.snp_right)
            $0.width.equalTo(80)
            $0.height.equalTo(30)
        }
        
        
        verificationCodeTF = UITextField.init()
        verificationCodeTF.isHidden = true
        verificationCodeTF.delegate = self
        verificationCodeTF.backgroundColor = .white
        verificationCodeTF.placeholder = "验证码"
        verificationCodeTF.keyboardType = .numberPad
        verificationCodeTF.textAlignment = .left
        verificationCodeTF.textColor = .black
        verificationCodeTF.font = .systemFont(ofSize: 16)
        view.addSubview(verificationCodeTF)
        verificationCodeTF.snp.makeConstraints{
            $0.top.equalTo(passwordView.snp_bottom).offset(30)
            $0.left.equalTo(view.snp_left).offset(50)
            $0.right.equalTo(verificationCodeGetBtn.snp_left).offset(-8)
            $0.height.equalTo(30)
        }
        
        
        verificationCodeView = UIView.init()
        verificationCodeView.isHidden = true
        verificationCodeView.backgroundColor = .lightGray
        view.addSubview(verificationCodeView)
        verificationCodeView.snp.makeConstraints{
            $0.left.bottom.right.equalTo(verificationCodeTF)
            $0.height.equalTo(1)
        }
        
    }
    
    @objc func btnClicked(sender:UIButton) {
        switch sender.tag {
        case 0:
            loginBtn.setTitleColor(.orange, for: .normal)
            registerBtn.setTitleColor(.lightGray, for: .normal)
            enterBtn.setTitle("登录", for: .normal)
            verificationCodeTF.isHidden = true
            verificationCodeView.isHidden = true
            verificationCodeGetBtn.isHidden = true
            print("你点击了登录")
        case 1:
            loginBtn.setTitleColor(.lightGray, for: .normal)
            registerBtn.setTitleColor(.orange, for: .normal)
            enterBtn.setTitle("注册", for: .normal)
            verificationCodeTF.isHidden = false
            verificationCodeView.isHidden = false
            verificationCodeGetBtn.isHidden = false
            print("你点击了注册")
        case 3:
            UserCenter.shareInstance.setLogin(login: true)
            self.dismiss(animated: true, completion: nil)
        default:
            //
            print("你点击了获取验证码")
        }
    }
    
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if textField == accountTF
        {
            accountView.backgroundColor = .orange
            passwordView.backgroundColor = .lightGray
            verificationCodeView.backgroundColor = .lightGray
        }
        if textField == passwordTF
        {
            accountView.backgroundColor = .lightGray
            passwordView.backgroundColor = .orange
            verificationCodeView.backgroundColor = .lightGray
        }
        if textField == verificationCodeTF
        {
            accountView.backgroundColor = .lightGray
            passwordView.backgroundColor = .lightGray
            verificationCodeView.backgroundColor = .orange
        }
        return true
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}


